

package cv.vender;

import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Date;
import java.util.List;

import cv.vender.para.clsADCPara;
import cv.vender.para.clsCalibrationPara;
import cv.vender.para.clsCardSnPara;
import cv.vender.para.clsCrossPara;
import cv.vender.para.clsHandShakePara;
import cv.vender.para.clsPcPollCreditPara;
import cv.vender.para.clsPcPollPara;
import cv.vender.para.clsPcSubCreditPara;
import cv.vender.para.clsPollTransfor;
import cv.vender.para.clsRealValPara;
import cv.vender.para.clsSetCashDevicePara;
import cv.vender.para.clsSetRelayPara;
import cv.vender.para.clsTradeMsgToMcu;
import cv.vender.para.clsTransforPara;
import android_serialport_api.SerialPort;

/**
 * 实现了所有和下位机通信的函数
 *
 * @author Administrator
 *
 */
public class YtMainBoard
{
	private static final byte STRATSIGN0= 'E';
	private static final byte STRATSIGN1= 'F';
	private static final byte VER= (byte)203;
	private static final String TAG = "YtMainBoard";
	private static short fid=0;
	private static short crc16_update(short crc, byte a)
	{
		int i;
		crc = (short)(crc ^a);
		for (i = 0; i < 8; ++i)
		{
			if ((crc & 1)!=0)
			{
				crc = (short)((short)((crc >> 1)) ^ (short)0xA001);
			}
			else
			{
				crc = (short)(crc >> 1);
			}
		}
		return crc;
	}


	private static short Slot_GetChk(byte[] bufData, int buflen)
	{
		int CRC = 0x0000ffff;
		int POLYNOMIAL = 0x0000a001;
		int i, j;

		for (i = 0; i < buflen; i++)
		{
			CRC ^= ((int)bufData[i] & 0x000000ff);
			for (j = 0; j < 8; j++)
			{
				if ((CRC & 0x00000001) != 0)
				{
					CRC >>= 1;
					CRC ^= POLYNOMIAL;
				}
				else
				{
					CRC >>= 1;
				}
			}
		}


		return (short)CRC;
	}





	private FileOutputStream mOutputStream;

	private FileInputStream mInputStream;

	private SerialPort sp;




	/**
	 * 打开通信串口
	 * @param comid 串口名称
	 * @param baudrate 波特率
	 * @return error code,0表示没有错误，非0表示有错误，错误代码请参见  {@link clsErrorConst}
	 */
	public int EF_OpenDev(String comid,int baudrate)
	{
		/*打开串口*/

		boolean isopen=false;
		try
		{
			sp=new SerialPort(new File(comid),baudrate,0);


			mOutputStream=(FileOutputStream) sp.getOutputStream();
			mInputStream=(FileInputStream) sp.getInputStream();

			isopen=true;
		}
		catch (SecurityException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		if(isopen)
		{
			return clsErrorConst.MDB_ERR_NO_ERR;
		}
		else
		{
			return clsErrorConst. MDB_ERR_CANT_OPEN;
		}
	}


	/**
	 * 关闭已经打开的通信串口
	 * @return error code,0表示没有错误，非0表示有错误，错误代码请参见  {@link clsErrorConst}
	 */
	public int EF_CloseDev()
	{
		boolean isclose=false;
		try
		{
			if(sp!=null)
			{
				sp.close();
				isclose=true;
				mOutputStream=null;
				mInputStream=null;
			}

		} catch (SecurityException e)
		{
			e.printStackTrace();
		}

		if(isclose)
		{
			return clsErrorConst.MDB_ERR_NO_ERR;
		}
		else
		{
			return clsErrorConst.MDB_ERR_CANT_CLOSE;
		}
	}

	clsFrame current_frame=null;

	private int SendFrame(byte cmd,byte[] data,int len)
	{

		current_frame=new clsFrame();

		byte[] buf=new byte[4096];
		int i=0;
		short chk;
		buf[i++]=STRATSIGN0;
		buf[i++]=STRATSIGN1;

		buf[i++]=VER;
		fid++;

		buf[i++]=(byte)(fid&0x00ff);
		buf[i++]=(byte)((fid>>8)&0x00ff);

		buf[i++]=cmd;

		buf[i++]=(byte)(len&0x00ff);
		buf[i++]=(byte)((len>>8)&0x00ff);

		if(len!=0)
		{
			System.arraycopy(data, 0, buf, i, len);

			i+=len;
		}
		chk= Slot_GetChk(buf,i);

		buf[i++]=(byte)(chk&0x00ff);
		buf[i++]=(byte)((chk>>8)&0x00ff);

		//clsToolBox.printX(buf, i);

		current_frame.setData(buf);
		current_frame.setLen(i);
		try {
			Thread.sleep(200);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		SendData(current_frame);


		return clsErrorConst.MDB_ERR_NO_ERR;
	}



	private void SendData(clsFrame frame2) {

		if(frame2!=null)
		{
			try {
				mInputStream.skip(mInputStream.available());
				mOutputStream.write(frame2.getData(), 0, frame2.getLen());

				try {
					Thread.sleep(frame2.getLen());
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Log.i(TAG,clsToolBox.getTimeLongString(new Date())+" SEND:");
				Log.i(TAG,clsToolBox.BytesToHexString(frame2.getData(),frame2.getLen()));

			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}



	private static final int FIX_LENGTH =10;

	private static final int INDEX_FRAME_DATA_LEN=6;
	private static final int INDEX_FRAME_DATA=8;
	private static final int INDEX_FRAME_CMD=5;
	@SuppressWarnings("unused")
	private static final int INDEX_FRAME_FID=3;

	@SuppressWarnings("unused")
	private static final int INDEX_FRAME_VER=2;

	private void delayTime(int max_time,int need_len,FileInputStream is) throws IOException
	{
		int over_time=0;

		while(is.available()<need_len)
		{
			try {
				Thread.sleep(100);

			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			over_time+=100;
			if(max_time<over_time)
			{
				Log.i(TAG, "overtime");
				break;
			}
		}
	}



	private int ReceFrame(clsFrame r_data)
	{
		byte[] b=new byte[2048];

		int err=clsErrorConst.MDB_ERR_NO_ERR;
		int rlen=0;
		int _max_wait_time=100;

		try
		{
			while(r_data.getTtl()>0)
			{
				r_data.subTtl();
				rlen=0;
				err=clsErrorConst.MDB_ERR_NO_ERR;

				_max_wait_time=r_data.getWaitTime();

				delayTime(_max_wait_time,FIX_LENGTH,mInputStream);
				int current_len=mInputStream.available();
				if(current_len==0)
				{
					err=clsErrorConst.MDB_ERR_TIME_OVER;
				}

				if(err==clsErrorConst.MDB_ERR_NO_ERR)
				{
					rlen=mInputStream.read(b, 0, FIX_LENGTH);

					if((b[0]!=STRATSIGN0)||(b[1]!=STRATSIGN1))
					{
						err=clsErrorConst.MDB_ERR_START_FLG;
					}
					else
					{
						int len=clsToolBox.little_byte2short(b, INDEX_FRAME_DATA_LEN)&0x0000ffff;
						/*等待若干时间*/

						if(len>=1500)
						{
							err=clsErrorConst.MDB_ERR_DATA_OVER;
						}
						else
						{
							long end_tick=System.currentTimeMillis()+2000;
							int need_len=len+FIX_LENGTH-rlen;
							while(need_len>0)
							{
								delayTime(need_len<100?100:need_len,need_len>500?500:need_len,mInputStream);
								if(mInputStream.available()>0)
								{
									rlen+=mInputStream.read(b,rlen,need_len);
								}
								else
								{
									if(end_tick<System.currentTimeMillis())
									{
										err=clsErrorConst.MDB_ERR_TIME_OVER;
										break;
									}
								}
								need_len=len+FIX_LENGTH-rlen;
							}
						}

						if(err==clsErrorConst.MDB_ERR_NO_ERR)
						{
							if(r_data.getCmd()!=b[INDEX_FRAME_CMD])
							{
								err= clsErrorConst.MDB_ERR_CMD_ERROR;
							}
						}

						if(err==clsErrorConst.MDB_ERR_NO_ERR)
						{
							if(Slot_GetChk(b,rlen)!=0)
							{
								err= clsErrorConst.MDB_ERR_CRC;
							}
						}
					}

					if(b[INDEX_FRAME_CMD]==clsCmdConst.CMD_PC_MES_ERR)
					{
						err= clsErrorConst.MDB_ERR_RET_ERROR;
					}
				}


				if(err!=clsErrorConst.MDB_ERR_NO_ERR)
				{
					//mInputStream.skip(mInputStream.available());

					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {

						e.printStackTrace();
					}

					Log.i(TAG, "Resend:"+err);
					SendData(current_frame);

					continue;
				}
				else
				{
					break;
				}

			}


			if(err==clsErrorConst.MDB_ERR_NO_ERR)
			{
				r_data.setLen(rlen);
				r_data.setData(b);
				Log.i(TAG, clsToolBox.getTimeLongString(new Date())+" RECE:");
				Log.i(TAG,clsToolBox.BytesToHexString(r_data.getData(),r_data.getLen()));
			}

		}
		catch (IOException e)
		{
			err=clsErrorConst.MDB_ERR_UNKOWEN;
			e.printStackTrace();

		}

		return err;
	}




	private static YtMainBoard instance=null;

	public static YtMainBoard getInstance() {

		if(instance==null)
		{
			instance=new YtMainBoard();


		}
		return instance;
	}




	public int EF_HandShake(clsHandShakePara para)
	{

		int e;

		byte cmd=(byte)clsCmdConst.CMD_PC_LNK;
		SendFrame(cmd, null, 0);

		clsFrame r_data=new clsFrame();
		r_data.setCmd(cmd);
		r_data.setTtl(5);
		e=ReceFrame(r_data);

		if(e==clsErrorConst.MDB_ERR_NO_ERR)
		{
			byte[] data=r_data.getData();
			para.setVer(clsToolBox.little_byte2int(data,INDEX_FRAME_DATA));
		}
		else if(e==clsErrorConst.MDB_ERR_TIME_OVER)
		{

		}
		else  if(e==clsErrorConst.MDB_ERR_CRC)
		{

		}
		else
		{

		}

		return e;
	}

	public int EF_PcPollCredit(clsPcPollCreditPara para)
	{

		int e;
		int i=0;

		byte cmd=(byte)clsCmdConst.CMD_PC_CREDIT;
		SendFrame(cmd, null, 0);
		clsFrame r_data=new clsFrame();
		r_data.setCmd(cmd);
		r_data.setTtl(5);
		e=ReceFrame(r_data);

		if(e==clsErrorConst.MDB_ERR_NO_ERR)
		{
			byte[] data=r_data.getData();
			i=INDEX_FRAME_DATA;
			para.setCredit(clsToolBox.little_byte2int(data,i));
			i+=4;
		}
		else if(e==clsErrorConst.MDB_ERR_TIME_OVER)
		{

		}
		else  if(e==clsErrorConst.MDB_ERR_CRC)
		{

		}
		else
		{

		}

		return e;
	}

	public int EF_PcClrCredit(clsPcPollCreditPara para)
	{
		int e;
		int i=0;
		byte cmd=(byte)clsCmdConst.CMD_PC_MDB_CLEAR_CREDIT;
		SendFrame(cmd, null, 0);
		clsFrame r_data=new clsFrame();
		r_data.setCmd(cmd);
		r_data.setTtl(5);
		e=ReceFrame(r_data);

		if(e==clsErrorConst.MDB_ERR_NO_ERR)
		{
			byte[] data=r_data.getData();
			i=INDEX_FRAME_DATA;
			para.setCredit(clsToolBox.little_byte2int(data,i));
			i+=4;
		}
		else if(e==clsErrorConst.MDB_ERR_TIME_OVER)
		{

		}
		else  if(e==clsErrorConst.MDB_ERR_CRC)
		{

		}
		else
		{

		}

		return e;
	}



	public int EF_PcSubCredit(clsPcSubCreditPara para)
	{
		int e;
		int i=0;
		byte[] s=new byte[8];
		byte cmd=(byte)clsCmdConst.CMD_PC_MDB_SUB_CREDIT;
		i=clsToolBox.intToByte(para.getSubCredit(),s,0);
		SendFrame(cmd,s,i);
		clsFrame r_data=new clsFrame();
		r_data.setCmd(cmd);
		r_data.setTtl(5);
		e=ReceFrame(r_data);

		if(e==clsErrorConst.MDB_ERR_NO_ERR)
		{
			byte[] data=r_data.getData();
			i=INDEX_FRAME_DATA;

			para.setSuccess(data[i]==1?true:false);

			para.setCredit(clsToolBox.little_byte2int(data,i));

			i+=4;
		}
		else if(e==clsErrorConst.MDB_ERR_TIME_OVER)
		{

		}
		else  if(e==clsErrorConst.MDB_ERR_CRC)
		{

		}
		else
		{

		}

		return e;
	}

	/**
	 * CMD_PC_LNK=0X02<br>
	 * 上位机查询下位机参数
	 * @param para 交换数据的参数对象
	 * @return error code,0表示没有错误，非0表示有错误，错误代码请参见  {@link clsErrorConst}
	 */
	public int EF_PcPoll(clsPcPollPara para)
	{
		int e;
		int i=0;

		byte cmd=(byte)clsCmdConst.CMD_PC_POLL;
		SendFrame(cmd, null, 0);

		clsFrame r_data=new clsFrame();
		r_data.setCmd(cmd);
		r_data.setTtl(5);
		e=ReceFrame(r_data);

		if(e==clsErrorConst.MDB_ERR_NO_ERR)
		{
			byte[] data=r_data.getData();
			i=INDEX_FRAME_DATA;
			para.setVenderid(clsToolBox.little_byte2int(data,i));
			i+=4;
			para.setEvent(clsToolBox.little_byte2int(data,i));
			i+=4;
			para.setMdb_coinAttube(clsToolBox.little_byte2int(data,i));
			i+=4;
			para.setCoin_state(clsToolBox.little_byte2int(data,i));
			i+=4;
			para.setBill_state(clsToolBox.little_byte2int(data,i));
			i+=4;
			para.setCurrent_credit(clsToolBox.little_byte2int(data,i));
			i+=4;
			//para.setMdb_coin_100(clsToolBox.little_byte2int(data,i));
			//i+=4;

		}
		else if(e==clsErrorConst.MDB_ERR_TIME_OVER)
		{

		}
		else  if(e==clsErrorConst.MDB_ERR_CRC)
		{

		}
		else
		{

		}

		return e;
	}

	public int EF_GetCrossVal(clsCrossPara para)
	{
		int e;
		int i=0;

		byte cmd=(byte)clsCmdConst.CMD_PC_GET_GROSS;
		SendFrame(cmd, null, 0);

		clsFrame r_data=new clsFrame();
		r_data.setCmd(cmd);
		r_data.setTtl(5);
		e=ReceFrame(r_data);

		if(e==clsErrorConst.MDB_ERR_NO_ERR)
		{
			byte[] data=r_data.getData();
			i=INDEX_FRAME_DATA;
			para.setPizong(clsToolBox.little_byte2int(data,i));
			i+=4;
		}
		else if(e==clsErrorConst.MDB_ERR_TIME_OVER)
		{

		}
		else  if(e==clsErrorConst.MDB_ERR_CRC)
		{

		}
		else
		{

		}
		return e;
	}

	public int EF_GetRealVal(clsRealValPara para)
	{
		int e;
		int i=0;

		byte cmd=(byte)clsCmdConst.CMD_PC_GET_WEIGHT;
		SendFrame(cmd, null, 0);

		clsFrame r_data=new clsFrame();
		r_data.setCmd(cmd);
		r_data.setTtl(5);
		e=ReceFrame(r_data);

		if(e==clsErrorConst.MDB_ERR_NO_ERR)
		{
			byte[] data=r_data.getData();
			i=INDEX_FRAME_DATA;
			para.setPizong(clsToolBox.little_byte2int(data,i));
			i+=4;
			para.setRealval(clsToolBox.little_byte2int(data,i));
			i+=4;
		}
		else if(e==clsErrorConst.MDB_ERR_TIME_OVER)
		{

		}
		else  if(e==clsErrorConst.MDB_ERR_CRC)
		{

		}
		else
		{

		}
		return e;
	}
	public int EF_GetAdcVal(clsADCPara para)
	{
		int e;
		int i=0;
		byte cmd=(byte)clsCmdConst.CMD_PC_READ_ADC;
		SendFrame(cmd, null, 0);

		clsFrame r_data=new clsFrame();
		r_data.setCmd(cmd);
		r_data.setTtl(5);
		e=ReceFrame(r_data);

		if(e==clsErrorConst.MDB_ERR_NO_ERR)
		{
			byte[] data=r_data.getData();
			i=INDEX_FRAME_DATA;
			para.setAdVal(clsToolBox.little_byte2int(data,i));
			i+=4;
		}
		else if(e==clsErrorConst.MDB_ERR_TIME_OVER)
		{

		}
		else  if(e==clsErrorConst.MDB_ERR_CRC)
		{

		}
		else
		{

		}
		return e;
	}

	/**
	 * CMD_PC_TRANSFOR=0x34<br>
	 * 出米
	 * @param para
	 * @return error code,0表示没有错误，非0表示有错误，错误代码请参见  {@link clsErrorConst}
	 */
	public int EF_DoTransfor(clsTransforPara para)
	{
		int e;
		byte[]  buf=new byte[256];
		int i=0;

		byte cmd=(byte)clsCmdConst.CMD_PC_TRANSFOR;
		clsToolBox.intToByte(para.getRiceAmount(),buf,i);/*出米的质量*/
		i+=4;
		SendFrame(cmd, buf, i);
		clsFrame r_data=new clsFrame();
		r_data.setCmd(cmd);
		r_data.setTtl(5);
		e=ReceFrame(r_data);
		if(e==clsErrorConst.MDB_ERR_NO_ERR)
		{

		}
		else if(e==clsErrorConst.MDB_ERR_TIME_OVER)
		{

		}
		else  if(e==clsErrorConst.MDB_ERR_CRC)
		{

		}
		else
		{

		}
		return e;
	}

	public int EF_SetRelay(clsSetRelayPara para)
	{
		int e;
		byte[]  buf=new byte[256];
		int i=0;

		byte cmd=(byte)clsCmdConst.CMD_PC_RELAY_TEST;
		clsToolBox.intToByte(para.getRelayid(),buf,i);/*出米的质量*/
		i+=4;
		clsToolBox.intToByte(para.isMswitch()?1:0,buf,i);
		i+=4;
		SendFrame(cmd, buf, i);
		clsFrame r_data=new clsFrame();
		r_data.setCmd(cmd);
		r_data.setTtl(5);
		e=ReceFrame(r_data);
		if(e==clsErrorConst.MDB_ERR_NO_ERR)
		{

		}
		else if(e==clsErrorConst.MDB_ERR_TIME_OVER)
		{

		}
		else  if(e==clsErrorConst.MDB_ERR_CRC)
		{

		}
		else
		{

		}
		return e;
	}

	public int EF_SetSetCashDevice(clsSetCashDevicePara para)
	{
		int e;
		byte[]  buf=new byte[256];
		int i=0;

		byte cmd=(byte)clsCmdConst.CMD_PC_SET_BILL_TYPE;
		clsToolBox.intToByte(para.getBills(),buf,i);
		i+=4;
		clsToolBox.intToByte(para.getCoins(),buf,i);
		i+=4;
		SendFrame(cmd, buf, i);
		clsFrame r_data=new clsFrame();
		r_data.setCmd(cmd);
		r_data.setTtl(5);
		e=ReceFrame(r_data);
		if(e==clsErrorConst.MDB_ERR_NO_ERR)
		{

		}
		else if(e==clsErrorConst.MDB_ERR_TIME_OVER)
		{

		}
		else  if(e==clsErrorConst.MDB_ERR_CRC)
		{

		}
		else
		{

		}
		return e;
	}

	/**
	 * CMD_PC_TRANSFOR=0x34<br>
	 * 出米
	 * @param para
	 * @return error code,0表示没有错误，非0表示有错误，错误代码请参见  {@link clsErrorConst}
	 */
	public int EF_PollTransfor(clsPollTransfor para)
	{
		int e;
		int i=0;
		byte cmd=(byte)clsCmdConst.CMD_PC_POLL_TRANSFOR;

		SendFrame(cmd, null, 0);
		clsFrame r_data=new clsFrame();
		r_data.setCmd(cmd);
		r_data.setTtl(5);
		e=ReceFrame(r_data);
		if(e==clsErrorConst.MDB_ERR_NO_ERR)
		{
			byte[] data=r_data.getData();
			i=INDEX_FRAME_DATA;
			para.setOutRice(clsToolBox.little_byte2int(data,i));
			i+=4;
			para.setState(clsToolBox.little_byte2int(data,i));
			i+=4;
		}
		else if(e==clsErrorConst.MDB_ERR_TIME_OVER)
		{

		}
		else  if(e==clsErrorConst.MDB_ERR_CRC)
		{

		}
		else
		{

		}
		return e;
	}

	/**
	 * CMD_PC_SET_CALIBRATION=0x51<br>
	 * 设置校正参数
	 * @param
	 * @return error code,0表示没有错误，非0表示有错误，错误代码请参见  {@link clsErrorConst}
	 */
	public int EF_SetCalibration(List<clsCalibrationPara> paralst)
	{
		int e;
		byte[] buf=new byte[256];
		int i=0;

		clsToolBox.intToByte(clsCalibrationPara.COUNT,buf,i);/*出米的质量*/
		i+=4;

		int l=4;
		int h=clsCalibrationPara.COUNT*4+4;
		int j=clsCalibrationPara.COUNT*4*2+4;
		for (clsCalibrationPara para:paralst)
		{
			clsToolBox.intToByte(para.getAdc(),buf,l);/**/
			l+=4;
			clsToolBox.intToByte(para.getK(),buf,h);/**/
			h+=4;
			clsToolBox.intToByte(para.getB(),buf,j);/**/
			j+=4;

			i+=12;
		}

		byte cmd=(byte)clsCmdConst.CMD_PC_SET_CALIBRATION;
		SendFrame(cmd, buf, i);
		clsFrame r_data=new clsFrame();
		r_data.setCmd(cmd);
		r_data.setTtl(5);
		e=ReceFrame(r_data);
		if(e==clsErrorConst.MDB_ERR_NO_ERR)
		{

		}
		else if(e==clsErrorConst.MDB_ERR_TIME_OVER)
		{

		}
		else if(e==clsErrorConst.MDB_ERR_CRC)
		{

		}
		else
		{

		}
		return e;
	}

	public int EF_GetCardSn(clsCardSnPara para) {

		int e;
		int i=0;
		byte cmd=(byte)clsCmdConst.CMD_PC_GET_CARDSN;

		SendFrame(cmd, null, 0);
		clsFrame r_data=new clsFrame();
		r_data.setCmd(cmd);
		r_data.setTtl(5);
		e=ReceFrame(r_data);
		if(e==clsErrorConst.MDB_ERR_NO_ERR)
		{
			byte[] data=r_data.getData();
			i=INDEX_FRAME_DATA;

			int tem=clsToolBox.little_byte2int(data,i);
			para.setSuccess(tem==0);
			i+=4;
			String sn= clsToolBox.AscByte2StringTerminalByNull(data,i);
			para.setCardSn(sn);
			i+=4;
		}
		else if(e==clsErrorConst.MDB_ERR_TIME_OVER)
		{

		}
		else  if(e==clsErrorConst.MDB_ERR_CRC)
		{

		}
		else
		{

		}
		return e;
	}


	public int EF_LoadTradeMsg(clsTradeMsgToMcu para) {

		int e;
		int i=0;
		byte cmd=(byte)clsCmdConst.CMD_PC_LOAD_TRADE_MSG;

		SendFrame(cmd, null, 0);
		clsFrame r_data=new clsFrame();
		r_data.setCmd(cmd);
		r_data.setTtl(5);
		e=ReceFrame(r_data);
		if(e==clsErrorConst.MDB_ERR_NO_ERR)
		{
			byte[] data=r_data.getData();
			i=INDEX_FRAME_DATA;

			para.setXuhao(clsToolBox.little_byte2short(data,i));
			i+=2;

			i=para.getTotal().FillParaFromBytes(data,i);

			i=para.getCash().FillParaFromBytes(data,i);

			i=para.getWeixin().FillParaFromBytes(data,i);

			i=para.getAlipay().FillParaFromBytes(data,i);


			para.setStart_timestamp(clsToolBox.little_byte2long(data,i));
			i+=8;
			System.out.println(para.getStart_timestamp());
			para.setEnd_timestamp(clsToolBox.little_byte2long(data,i));
			i+=8;
		}
		else if(e==clsErrorConst.MDB_ERR_TIME_OVER)
		{

		}
		else  if(e==clsErrorConst.MDB_ERR_CRC)
		{

		}
		else
		{

		}
		return e;
	}


	public int EF_SaveTradeMsg(clsTradeMsgToMcu para)
	{
		int e;
		int i=0;
		byte cmd=(byte)clsCmdConst.CMD_PC_SAVE_TRADE_MSG;
		byte[] buf=new byte[128];

		i=clsToolBox.shortToByte((short) para.getXuhao(),buf,i);
		i=para.getTotal().FillParaToBytes(buf,i);
		i=para.getCash().FillParaToBytes(buf,i);
		i=para.getWeixin().FillParaToBytes(buf,i);
		i=para.getAlipay().FillParaToBytes(buf,i);

		System.out.println(para.getStart_timestamp());
		i=clsToolBox.longToByte(para.getStart_timestamp(),buf,i);
		i=clsToolBox.longToByte(para.getEnd_timestamp(),buf,i);

		SendFrame(cmd, buf, i);
		clsFrame r_data=new clsFrame();
		r_data.setCmd(cmd);
		r_data.setTtl(5);
		e=ReceFrame(r_data);
		if(e==clsErrorConst.MDB_ERR_NO_ERR)
		{

		}
		else if(e==clsErrorConst.MDB_ERR_TIME_OVER)
		{

		}
		else  if(e==clsErrorConst.MDB_ERR_CRC)
		{

		}
		else
		{

		}
		return e;
	}
}
